package com.tarena.lbs.user.service.impl;

import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.dao.repository.UserTagsRepository;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserService;
import com.tarena.lbs.user.utils.RandomUserName;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserTagsRepository userTagsRepository;
    @Autowired
    private UserGroupRepository userGroupRepository;


    @Override
    public void register(UserParam param) throws BusinessException {
        String phone = param.getPhone();
        Long count = userRepository.countUserByPhone(phone);
        Asserts.isTrue(count>0,new BusinessException("-2","手机号已经存在"));
        //2.封装数据
        UserPO poParam = assembleUserPO(param);
        //3.直接写入数据库
        userRepository.save(poParam);
    }

    @Override
    public UserVO detail() throws BusinessException {
        //1.解析jwt 拿到userPrinciple
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        Integer userId = userPrinciple.getId();
        //2.仓储层查询唯一的po
        UserPO po = userRepository.getById(userId);
        Asserts.isTrue(po==null,new BusinessException("-2","用户信息不存在"));
        //3.转化vo
        UserVO vo=new UserVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    @Override
    public void bindUserTags(UserTagsParam param) throws BusinessException {
        // 校验用户是否登录
        //1.解析jwt 拿到userPrinciple
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        Integer id = userPrinciple.getId();
        //2.仓储层查询唯一的po
        UserPO po = userRepository.getById(id);
        Asserts.isTrue(po==null,new BusinessException("-2","用户信息不存在"));
        // 业务实现
        log.debug("用户标签关联,入参:param={}",param);
        Integer userId = po.getId();
        String tagIds = param.getTagIds();
        String[] ids = tagIds.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String tagId : ids) {
            idList.add(Integer.parseInt(tagId));
        }
        userTagsRepository.insert(userId,idList);
    }

    @Override
    public List<Integer> getUserGroupIds(Integer userId, Integer businessId) {
        log.debug("根据用户id和商家id查询用户分组id列表业务,入参:userId={},businessId={}",userId,businessId);
        List<Integer> groupIds = new ArrayList<>();
        List<UserTagsPO> userTagsPOS = userTagsRepository.queryByUserId(userId);
        if (CollectionUtils.isEmpty(userTagsPOS)){
            return groupIds;
        }
        List<Integer> tagIds = new ArrayList<>();
        for (UserTagsPO userTagsPO : userTagsPOS) {
            tagIds.add(userTagsPO.getTagId());
        }
        List<UserGroupPO> groupPOS = userGroupRepository.selectByBusinessIdAndTagIds(businessId,tagIds);
        for (UserGroupPO groupPO : groupPOS) {
            groupIds.add(groupPO.getId());
        }
        log.debug("根据用户id和商家id查询用户分组id列表业务,出参:groupIds={}",groupIds);
        return groupIds;
    }

    private UserPO assembleUserPO(UserParam param) {
        UserPO po=new UserPO();
        BeanUtils.copyProperties(param,po);
        //补充必要数据 用户状态 0表示没有绑定标签 1 表示绑定了标签
        po.setStatus(0);
        //用户昵称 注册时候 不是用户填写的 为了快速注册 我们随机生成一个
        String randomName = RandomUserName.createRandomName();//老 大 小  张王李赵
        po.setNickName(randomName);
        po.setUserName(randomName);
        //注册时间
        po.setRegTime(new Date());
        return po;
    }

}
